//
//  NetWorkingTools.swift
//  swift_demo_weibo
//
//  Created by 杨兵 on 15/5/17.
//  Copyright (c) 2015年 bing. All rights reserved.
//  建立网络连接层

import UIKit
import Alamofire
import SVProgressHUD

class NetWorkingTools: NSObject {
    
    // 定义请求JOSN的方法
    class func requestJSON(method: Alamofire.Method, URLString: String, parameters: [String : AnyObject]? = nil, complete:(JSON : AnyObject?) -> ()) {
        
        Alamofire.request(method, URLString, parameters: parameters).responseJSON(){ (_, _, JSON, error) -> Void in
            if error != nil {
                SVProgressHUD.showErrorWithStatus("网络访问失败")
                return
            }
            // 回调数据
            complete(JSON: JSON)
            
        }
    
    }
    
    
    
    // 上传图片
    static let boundary = "asdahsdhashdkahsdkakshk"
    class func uploadPicture(URLString: String, fieldName: String, datalist:[String: NSData!], parameters: [String: AnyObject]? = nil, complete:(JSON : AnyObject?) -> ()) {
        var request = NSMutableURLRequest(URL: NSURL(string: URLString)!)
        
        // 设置请求头
        request.HTTPMethod = "POST"
        // 设置请求类型
        let type = "multipart/form-data; boundary=\(boundary)"
        request.setValue(type, forHTTPHeaderField: "Content-Type")
        
        let data = formData(fieldName, datalist: datalist, parameters: parameters)
        
        // 请求数据
        NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: data) { (resposeData, _, _) -> Void in
            let result = NSJSONSerialization.JSONObjectWithData(resposeData, options: NSJSONReadingOptions(0), error: nil) as! NSDictionary
            complete(JSON: result)
        }.resume()
        
    }
    
    class func formData(fieldName: String, datalist:[String: NSData!], parameters: [String: AnyObject]? = nil) -> NSData{
        
        var dataM = NSMutableData()
        
        // 1.变量字典,拼接字符串数组
        if let params = parameters {
            for (k,v) in params {
                var strM = "--\(boundary)\r\n"
                strM += "Content-Disposition: form-data; name=\"\(k)\"\r\n\r\n"
                strM += "\(v)\r\n"

                dataM.appendData(strM.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
            }
        
        }
        
        // 2.拼接文件
        for (k,v) in datalist {
            var strM = "--\(boundary)\r\n"
            strM += "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(k)\"\r\n"
            strM += "Content-Type: application/octet-stream\r\n\r\n"
            
            dataM.appendData(strM.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
            dataM.appendData(v)
            dataM.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
            
        }
        
        // 3.标示请求结束
        let endstring = "--\(boundary)--\r\n"
        dataM.appendData(endstring.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
        
        
        return dataM
        
    }
    
    
}
















